package com.ydy.service.impl.center;

import com.ydy.enums.OrderStatusEnum;
import com.ydy.enums.YesOrNo;
import com.ydy.mapper.OrderStatusMapper;
import com.ydy.mapper.OrdersMapper;
import com.ydy.pojo.OrderStatus;
import com.ydy.pojo.Orders;
import com.ydy.pojo.vo.MyOrdersVO;
import com.ydy.pojo.vo.OrderStatusCountsVO;
import com.ydy.service.center.MyOrdersService;
import com.ydy.utils.PageInfo;
import com.ydy.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MyOrdersServiceImpl  implements MyOrdersService {

    public static final Logger log =
            LoggerFactory.getLogger(MyOrdersServiceImpl.class);

    @Autowired
    public OrdersMapper ordersMapper;

    @Autowired
    public OrderStatusMapper orderStatusMapper;


    /**
     * 根据用户id和订单状态查询我的订单列表
     * @param userId 用户id
     * @param orderStatus 订单状态
     * @param pageInfo 分页对象
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<MyOrdersVO> queryMyOrders(String userId, Integer orderStatus, PageInfo pageInfo) {

        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        if (orderStatus != null) {
            map.put("orderStatus", orderStatus);
        }

        PageUtil.StartPage(pageInfo);
        log.info("根据用户id:{}和订单状态:{}查询我的订单列表",userId,orderStatus);
        List<MyOrdersVO> list = ordersMapper.queryMyOrders(map);
        return list;
    }

    /**
     * 订单状态 --> 商家发货
     * @param orderId 订单id
     */
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateDeliverOrderStatus(String orderId) {
        OrderStatus updateOrder = new OrderStatus();
        updateOrder.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
        updateOrder.setDeliverTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
        log.info("根据订单id:{}更新订单为状态:已发货",orderId);
        orderStatusMapper.updateByExampleSelective(updateOrder, example);
    }

    /**
     * 验证用户和订单是否匹配
     * @param userId 用户id
     * @param orderId 订单id
     * @return
     */
    @Transactional(propagation=Propagation.SUPPORTS)
    @Override
    public Orders checkUserOrder(String userId, String orderId) {
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setId(orderId);
        orders.setIsDelete(YesOrNo.NO.type);
        log.info("根据订单id:{}和用户id:{}",orderId,userId);
        return ordersMapper.selectOne(orders);
    }

    /**
     * 更新订单状态 —> 确认收货
     * @param orderId 订单id
     * @return
     */
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public boolean updateReceiveOrderStatus(String orderId) {
        OrderStatus updateOrder = new OrderStatus();
        updateOrder.setOrderStatus(OrderStatusEnum.SUCCESS.type);
        updateOrder.setSuccessTime(new Date());

        Example example = new Example(OrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
        log.info("根据订单id:{}更新订单为状态:确认收货",orderId);
        int result = orderStatusMapper.updateByExampleSelective(updateOrder, example);

        return result == 1 ? true : false;
    }

    /**
     * 删除订单（逻辑删除）
     * @param userId 用户id
     * @param orderId 订单id
     * @return
     */
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public boolean deleteMyOrder(String userId, String orderId) {
        Orders updateOrder = new Orders();
        updateOrder.setIsDelete(YesOrNo.YES.type);
        updateOrder.setUpdatedTime(new Date());

        Example example = new Example(Orders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", orderId);
        criteria.andEqualTo("userId", userId);
        log.info("根据订单id:{}删除订单",orderId);
        int result = ordersMapper.updateByExampleSelective(updateOrder, example);

        return result == 1 ? true : false;
    }

    /**
     * 获得订单状态数概况
     * @param userId 用户id
     * @return
     */
    @Override
    public OrderStatusCountsVO getOrderStatusCounts(String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);

        map.put("orderStatus", OrderStatusEnum.WAIT_PAY.type);
        int waitPayCounts = ordersMapper.getMyOrderStatusCounts(map);

        map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
        int waitDeliverCounts = ordersMapper.getMyOrderStatusCounts(map);

        map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
        int waitReceiveCounts = ordersMapper.getMyOrderStatusCounts(map);

        map.put("orderStatus", OrderStatusEnum.SUCCESS.type);
        map.put("isComment", YesOrNo.NO.type);
        int waitCommentCounts = ordersMapper.getMyOrderStatusCounts(map);

        OrderStatusCountsVO countsVO = new OrderStatusCountsVO(waitPayCounts,
                waitDeliverCounts,
                waitReceiveCounts,
                waitCommentCounts);
        return countsVO;
    }

    /**
     * 查询订单动向
     * @param userId 用户id
     * @param pageInfo 分页对象
     * @return
     */
    @Override
    public List<OrderStatus> getOrdersTrend(String userId, PageInfo pageInfo) {
        PageUtil.StartPage(pageInfo);
        List<OrderStatus> list = ordersMapper.getMyOrderTrend(userId);
        return list;
    }
}
